package com.cn.gmall.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cn.gmall.common.util.MD5;
import com.cn.gmall.model.user.UserAddress;
import com.cn.gmall.model.user.UserInfo;
import com.cn.gmall.user.mapper.UserAddressMapper;
import com.cn.gmall.user.service.UserService;
import com.cn.gmall.user.mapper.UserInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
@SuppressWarnings("all")
public class UserServiceImpl implements UserService {

    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    UserAddressMapper userAddressMapper;

    /***
     * 获取用户地址
     * @param userId
     * @return
     */
    @Override
    public List<UserAddress> findUserAddressListByUserId(Long userId) {
        return userAddressMapper.selectList(new LambdaQueryWrapper<UserAddress>().eq(UserAddress::getUserId,userId));
    }

    /**
     * 登录
     * @param userInfo
     * @return
     */
    @Override
    public UserInfo longIn(UserInfo userInfo) {
//        密码MD5加密
        String encrypt = MD5.encrypt(userInfo.getPasswd());
        System.out.println("encrypt = " + encrypt);
        String s = DigestUtils.md5DigestAsHex(userInfo.getPasswd().getBytes());
        System.out.println("s = " + s);



     return userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getPasswd,MD5.encrypt(userInfo.getPasswd()))
                .eq(UserInfo::getLoginName, userInfo.getLoginName()));
    }
}
